#include"Dlish.h"
#include<stdio.h>
#include<stdlib.h>

typedef struct node{
    int data;
    struct list_head list;
}linknode, *linklist;

//初始化内核链表
linklist init_list(){
    linklist head = malloc(sizeof(linknode));
    if(head != NULL)
        INIT_LIST_HEAD(&head->list);
    return head;
}

linklist new_node(int data){
    linklist new = malloc(sizeof(linknode));
    if(new != NULL){
        new->data = data;
        INIT_LIST_HEAD(&new->list);
    }
    return new;
}

void show_list(linklist head){
    struct list_head *pos;
    list_for_each(pos, &head->list){
        linklist data = list_entry(pos, linknode, list);
        printf("%d ", data->data);
    }
    printf("\n");
}

void reverst(linklist head){
    struct list_head *pos;
    struct list_head *p = pos;
    int flag = 1;
    list_for_each_prev(pos, &head->list){
        if(flag == 1 && list_entry(pos, linknode, list)->data % 2 == 0){
            while(list_entry(pos, linknode, list)->data % 2 == 0)pos = pos->prev;
            flag = 0;
        }
        linklist data = list_entry(pos, linknode, list);
        if(data->data % 2 == 0){
            list_del(pos);
            list_add_tail(pos, &head->list);
            pos = p;
        }
        else p = pos;
    }
}

int main(int argc, char const *argv[])
{
    linklist head = init_list();
    struct list_head *pos;
    for(int i = 1; i <= 10; i++){
        list_add_tail(&new_node(i)->list, &head->list);
    }
    show_list(head);
    reverst(head);
    show_list(head);
    return 0;
}
